package com.weiadmin.controller.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.weiadmin.common.BaseController;
import com.weiadmin.dao.UserDao;
import com.weiadmin.repository.UserRepository;
import com.weiadmin.service.UserService;
import com.weiadmin.validate.LoginValidate;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共类
 *
 * @author: shizhongyu
 * @date: 2023年05月27日 17:25
 */
@RestController
@RequestMapping("/admin/public")
public class PublicController extends BaseController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserDao userMapper;

    @Value("${file.upload.path}")
    private String filePath;

    @Value("${file.upload.path.relative}")
    private String fileRelativePath;

    private static final String ACCESS_PATH = "http://localhost:8088/images/";
    @Autowired
    private UserService userService;
    @GetMapping("/login")
    public ModelAndView login(HttpSession session) {
        String userInfo  = (String) session.getAttribute("userInfo");
        if(userInfo!=null){
            System.out.println("userInfo"+userInfo);
            return redirect("/admin/index/index");
        }else{
            System.out.println("session 暂无相关数值");
            return display("login");
        }
    }

    @PostMapping("/doLogin")
    public ResponseEntity<?> doLogin(HttpSession session, @Validated LoginValidate request){
        int result = userService.doLogin(session,request);
        if(result==0){
            return error("账号或密码错误！");
        }else{
            return success("登录成功！");
        }
    }

    @PostMapping("/logout")
    public ResponseEntity<?> logout(HttpSession session) {
        session.removeAttribute("userInfo");
        return success("退出成功！");
    }
    @GetMapping("/password")
    public ResponseEntity<?> password(){
        String md5Hex1 = DigestUtil.md5Hex("admin888");
        String pinyin = PinyinUtil.getPinyin("你好", " ");
        return success("password:"+pinyin);
    }
    @GetMapping("/config")
    public ResponseEntity<?> config(){
        Map<String,String> map = new HashMap<>();
        map.put("configName","系统消息");
        map.put("configType","1");
        map.put("configValue","1");
        map.put("remark","测试备注");
        map.put("createTime","1660558068");

        Map<String,String> map1 = new HashMap<>();
        map1.put("configName","系统消息");
        map1.put("configType","1");
        map1.put("configValue","1");
        map1.put("remark","测试备注");
        map1.put("createTime","1660558068");

        ArrayList<Map<String,String>> list = new ArrayList<Map<String,String>>();
        list.add(map);
        list.add(map1);

        return success("请求成功!",list);
    }
    @GetMapping("/log")
    public ResponseEntity<?> log()
    {
        String json = "[{\"id\":\"1305106851892822016\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T11:31:36.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305101593019940864\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T11:10:42.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305101247900024832\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T11:09:20.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305099674977304576\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T11:03:05.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305095186061197312\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T10:45:15.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305092027951611904\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T10:32:42.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305082862634008576\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T09:56:16.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305071134877679616\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T09:09:40.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305070481803575296\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T09:07:05.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"},{\"id\":\"1305064691009060864\",\"title\":\"登录\",\"description\":\"登录成功\",\"businessType\":\"OTHER\",\"requestMethod\":\"POST\",\"method\":\"/login\",\"operateUrl\":\"/login\",\"operateAddress\":\"127.0.0.1\",\"requestParam\":null,\"requestBody\":\"\",\"responseBody\":null,\"success\":true,\"loggingType\":\"LOGIN\",\"errorMsg\":null,\"systemOs\":\"Windows\",\"createTime\":\"2020-09-13T08:44:04.000+0000\",\"operateName\":\"admin\",\"map\":{},\"browser\":\"谷歌浏览器\"}]";
        JSONArray jsonObject = JSONUtil.parseArray(json);
        return success("请求成功",jsonObject);
    }

    @PostMapping("/upload")
    public ResponseEntity<?>upload(MultipartFile file)
    {
        if(file==null){
            return error("文件不能为空！");
        }
        if (file.isEmpty()) {
            return error("文件不能为空！");
        }
        String fileName = file.getOriginalFilename();
        // 生成新文件名
        String suffixName = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".")) : null;
        String newName = IdUtil.randomUUID() + suffixName;
        try {
            // 重命名文件
            File newFile = new File(filePath, newName);
            // 如果该存储路径不存在则新建存储路径
            if (!newFile.getParentFile().exists()) {
                newFile.getParentFile().mkdirs();
            }
            // 文件写入
            file.transferTo(newFile);

        }catch (Exception e) {
            return error("上传出错,请稍后重试！");
        }
        Map<String,String> map = new HashMap<>();
        map.put("url",ACCESS_PATH  + newName);
        map.put("path", newName);
        map.put("path", newName);
       return success("上传成功！",map );
    }

    @PostMapping("/editorUpload")
    public ResponseEntity<?>editorUpload(MultipartFile file)
    {
        if(file==null){
            return error("文件不能为空！");
        }
        if (file.isEmpty()) {
            return error("文件不能为空！");
        }
        String fileName = file.getOriginalFilename();
        // 生成新文件名
        String suffixName = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".")) : null;
        String newName = IdUtil.randomUUID() + suffixName;
        try {
            // 重命名文件
            File newFile = new File(filePath, newName);
            // 如果该存储路径不存在则新建存储路径
            if (!newFile.getParentFile().exists()) {
                newFile.getParentFile().mkdirs();
            }
            // 文件写入
            file.transferTo(newFile);

        }catch (Exception e) {
            return error("上传出错,请稍后重试！");
        }
        Map<String,String> map = new HashMap<>();
        map.put("url",ACCESS_PATH  + newName);
        map.put("path", newName);
        map.put("path", newName);
        return success("上传成功！",map );
    }
}
